﻿using System;
using System.Data;
using System.Text;
using System.Data.SQLite;

namespace Clipper.DAL
{
	/// <summary>
	/// 数据访问类:TestTubeSelfInfo
	/// </summary>
	public partial class TestTubeSelfInfo
	{
		public TestTubeSelfInfo()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return SQLiteHelper.GetMaxID("testTubeSelfID", "TestTubeSelfInfo"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int testTubeSelfID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from TestTubeSelfInfo");
			strSql.Append(" where testTubeSelfID=@testTubeSelfID ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@testTubeSelfID", DbType.Int32,4)			};
			parameters[0].Value = testTubeSelfID;

			return SQLiteHelper.Exists(strSql.ToString(),parameters);
		}
        /// <summary>
        /// 是否存在该序列号
        /// </summary>
        /// <param name="barcode"></param>
        /// <returns></returns>
        public bool ExistBarcode(string barcode)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from TestTubeSelfInfo");
            strSql.Append(" where barcode=@barcode");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@barcode", DbType.String,20)			};
            parameters[0].Value = barcode;

            return SQLiteHelper.Exists(strSql.ToString(), parameters);
        }
		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(Clipper.Model.TestTubeSelfInfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into TestTubeSelfInfo(");
			strSql.Append("barcode,TestTubeSelfState,checkDateTime,note)");
			strSql.Append(" values (");
			strSql.Append("@barcode,@TestTubeSelfState,@checkDateTime,@note)");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@barcode", DbType.String,20),
					new SQLiteParameter("@TestTubeSelfState", DbType.Int32,4),
					new SQLiteParameter("@checkDateTime", DbType.Date),
					new SQLiteParameter("@note", DbType.String)};
			parameters[0].Value = model.barcode;
			parameters[1].Value = model.TestTubeSelfState;
			parameters[2].Value = model.checkDateTime;
			parameters[3].Value = model.note;

			int rows=SQLiteHelper.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool AddOrReplace(Clipper.Model.TestTubeSelfInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert or replace into TestTubeSelfInfo(");
            strSql.Append("barcode,TestTubeSelfState,checkDateTime,note)");
            strSql.Append(" values (");
            strSql.Append("@barcode,@TestTubeSelfState,@checkDateTime,@note)");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@barcode", DbType.String,20),
					new SQLiteParameter("@TestTubeSelfState", DbType.Int32,4),
					new SQLiteParameter("@checkDateTime", DbType.Date),
					new SQLiteParameter("@note", DbType.String)};
            parameters[0].Value = model.barcode;
            parameters[1].Value = model.TestTubeSelfState;
            parameters[2].Value = model.checkDateTime;
            parameters[3].Value = model.note;

            int rows = SQLiteHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Clipper.Model.TestTubeSelfInfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update TestTubeSelfInfo set ");
			strSql.Append("barcode=@barcode,");
			strSql.Append("TestTubeSelfState=@TestTubeSelfState,");
			strSql.Append("checkDateTime=@checkDateTime,");
			strSql.Append("note=@note");
			strSql.Append(" where testTubeSelfID=@testTubeSelfID ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@barcode", DbType.String,20),
					new SQLiteParameter("@TestTubeSelfState", DbType.Int32,4),
					new SQLiteParameter("@checkDateTime", DbType.Date),
					new SQLiteParameter("@note", DbType.String),
					new SQLiteParameter("@testTubeSelfID", DbType.Int32,4)};
			parameters[0].Value = model.barcode;
			parameters[1].Value = model.TestTubeSelfState;
			parameters[2].Value = model.checkDateTime;
			parameters[3].Value = model.note;
			parameters[4].Value = model.testTubeSelfID;

			int rows=SQLiteHelper.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int testTubeSelfID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from TestTubeSelfInfo ");
			strSql.Append(" where testTubeSelfID=@testTubeSelfID ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@testTubeSelfID", DbType.Int32,4)			};
			parameters[0].Value = testTubeSelfID;

			int rows=SQLiteHelper.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ShelfBarcode"></param>
        /// <returns></returns>
        public bool Delete(string ShelfBarcode)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from TestTubeSelfInfo ");
            strSql.Append(" where barcode=@testTubeSelfBarcode ");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@testTubeSelfBarcode", DbType.Int32,4)			};
            parameters[0].Value = ShelfBarcode;

            int rows = SQLiteHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string testTubeSelfIDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from TestTubeSelfInfo ");
			strSql.Append(" where testTubeSelfID in ("+testTubeSelfIDlist + ")  ");
			int rows=SQLiteHelper.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Clipper.Model.TestTubeSelfInfo GetModel(int testTubeSelfID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select testTubeSelfID,barcode,TestTubeSelfState,checkDateTime,note from TestTubeSelfInfo ");
			strSql.Append(" where testTubeSelfID=@testTubeSelfID ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@testTubeSelfID", DbType.Int32,4)			};
			parameters[0].Value = testTubeSelfID;

			Clipper.Model.TestTubeSelfInfo model=new Clipper.Model.TestTubeSelfInfo();
			DataSet ds=SQLiteHelper.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Clipper.Model.TestTubeSelfInfo DataRowToModel(DataRow row)
		{
			Clipper.Model.TestTubeSelfInfo model=new Clipper.Model.TestTubeSelfInfo();
			if (row != null)
			{
				if(row["testTubeSelfID"]!=null && row["testTubeSelfID"].ToString()!="")
				{
					model.testTubeSelfID=int.Parse(row["testTubeSelfID"].ToString());
				}
				if(row["barcode"]!=null)
				{
					model.barcode=row["barcode"].ToString();
				}
				if(row["TestTubeSelfState"]!=null && row["TestTubeSelfState"].ToString()!="")
				{
					model.TestTubeSelfState=int.Parse(row["TestTubeSelfState"].ToString());
				}
				if(row["checkDateTime"]!=null && row["checkDateTime"].ToString()!="")
				{
					model.checkDateTime=DateTime.Parse(row["checkDateTime"].ToString());
				}
				if(row["note"]!=null)
				{
					model.note=row["note"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select testTubeSelfID,barcode,TestTubeSelfState,checkDateTime,note ");
			strSql.Append(" FROM TestTubeSelfInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return SQLiteHelper.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM TestTubeSelfInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = SQLiteHelper.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.testTubeSelfID desc");
			}
			strSql.Append(")AS Row, T.*  from TestTubeSelfInfo T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return SQLiteHelper.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@tblName", DbType.VarChar, 255),
					new SQLiteParameter("@fldName", DbType.VarChar, 255),
					new SQLiteParameter("@PageSize", DbType.Int32),
					new SQLiteParameter("@PageIndex", DbType.Int32),
					new SQLiteParameter("@IsReCount", DbType.bit),
					new SQLiteParameter("@OrderType", DbType.bit),
					new SQLiteParameter("@strWhere", DbType.VarChar,1000),
					};
			parameters[0].Value = "TestTubeSelfInfo";
			parameters[1].Value = "testTubeSelfID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return SQLiteHelper.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

